<template>
  <div>
<!--    <h1 style="margin-left: 20px;">物业员工管理</h1>-->


    <el-form :inline="true" :model="keyword" ref="searchForm" class="demo-form-inline"
    style="padding-top: 10px;margin-left: 35px">
      <el-form-item prop="staffName">
        <el-input type="hidden" v-model="keyword.staffName" placeholder="员工姓名"></el-input>
      </el-form-item>
      <el-form-item prop="staffName" label="员工姓名:">
        <el-input v-model="keyword.staffName" placeholder="请输入完整员工姓名"></el-input>
      </el-form-item>

      <el-form-item prop="jobCategory" label="工作类型:">
      <el-select v-model="keyword.jobCategory"  placeholder="工作类型" @change="search" style="width:200px">
        <el-option v-for="roleType in ListKuang" :label="roleType" :value="roleType"/>
      </el-select>

      <!--查询-->
      <el-form-item>
      <el-button style="margin-left: 15px" type="primary" @click="search" size="small" icon="el-icon-search">搜索</el-button>
        <el-button type="info" @click="resetSearchForm('searchForm')" size="small" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
      </el-form-item>


      <el-form-item>
        <el-button style="margin-left: 30px" type="success" size="small" icon="el-icon-plus" @click="openDialog">添加员工</el-button>
        <el-button style="margin-left: 5px" type="danger" size="small" icon="el-icon-delete" @click="delBatch">批量删除</el-button>
      </el-form-item>

      <el-form-item>
        <!-- <el-button style="margin-left: 20px;" type="primary" size="small" icon="el-icon-upload2" @click="dialogExcelVisible = true">导入</el-button> -->
        <el-button style="margin-left: 35px;" type="warning" size="small" icon="el-icon-download" @click="downloadExcel">导出</el-button>
      </el-form-item>
    </el-form>

    <!-- 导入 -->
    <el-dialog title="上传物业人员管理信息Excel" :visible.sync="dialogExcelVisible" width="40%">
            <el-upload class="upload-demo" drag
                action="http://localhost:8081/psm/staff/upload/excel"
                accept=".xlsx,.xls" :on-success="uploadExcelSuccess" :on-error="uploadExcelError" multiple>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip" slot="tip">只能上传.xlsx或.xls文件, 且不超过500kb</div>
            </el-upload>
        </el-dialog>

    <!--弹框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="courseForm" :rules="stashRecodesRules" ref="courseRulesForm">
        <el-form-item label="员工名称" prop="staffName">
          <el-input v-model="courseForm.staffName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工性别">
          <el-radio-group v-model="courseForm.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="员工职位">
          <el-radio-group v-model="courseForm.jobCategory">
            <el-radio label="保洁"></el-radio>
            <el-radio label="维修"></el-radio>
            <el-radio label="物业"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="联系方式" prop="contactNumber">
          <el-input v-model="courseForm.contactNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="courseForm.idNumber" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <!--   点确定触发函数  进行校验 -->
        <el-button type="primary" @click="addCourse('courseRulesForm')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 表格数据区域-->
    <el-table :data="courseList" @selection-change="handleSelectionChange"
    row-key="staffId" style="width: 100%;margin-bottom: 20px;">
    <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="staffId" align="center" label="编号"></el-table-column>
      <el-table-column prop="staffName" align="center" label="员工名称"></el-table-column>
      <el-table-column prop="contactNumber" align="center" label="联系电话"></el-table-column>
      <el-table-column prop="idNumber" align="centIer" label="身份证号"></el-table-column>
      <el-table-column prop="gender" align="center" label="性别"></el-table-column>
      <el-table-column prop="jobCategory" align="center" label="工作类型"></el-table-column>
      <el-table-column prop="hireDate" align="center" label="聘用时间"></el-table-column>
      <el-table-column prop="quitDate" align="center" label="离职时间">
        <template slot-scope="scope">
          <span>{{ scope.row.quitDate || '暂无' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >解雇
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--完整功能分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 5, 7, 10,15,20]"
      :page-size="7"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

  </div>

</template>

<script>
import { getList, add, edit, del, findList, delBatch } from '@/api/psm/staff'

export default {
  name: 'index',
  data() {
    return {
      title: '',
      keyword: { // 添加查询关键词对象
        staffId: '',
        staffName: null,
        jobCategory: null,
      },
      loading: false,
      dialogExcelVisible: false,
      dialogFormVisible: false,
      stashRecodesRules: {
        staffName: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择员工性别', trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        idNumber: [
          { required: true, message: '请输入身份证号', trigger: 'blur' }
        ]
      },
      courseForm: {
        staffId: '',
        staffName: '',
        contactNumber: '',
        idNumber: '',
        gender: '',
        jobCategory: '',
        hireDate: '',
        quitDate: '',
        comments: '',
        ownerId: '',
      },
      ids: [],
      ListKuang: [],
      courseList: [],
      currentPage: 1,
      pageNum: 1,
      pageSize: 7,
      total: 0,
    }
  },
  methods: {
    findKuang() {
      findList().then(res => {
        this.ListKuang = res.data
        this.fetchData()
      })
    },
    fetchData() {
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        staffId: this.keyword.staffId,
        staffName: this.keyword.staffName,
        jobCategory: this.keyword.jobCategory
      }
      getList(params).then(res => {
        let pageInfo = res.data
        this.loading = false
        this.courseList = pageInfo.list
        this.total = pageInfo.total
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.fetchData()
    },
    // 搜索
    search() {
      this.fetchData()
    },
    // 重置
    resetSearchForm(formName) {
      this.$refs[formName].resetFields()
      this.fetchData()
    },
    // 添加
    openDialog() {
      this.courseForm = {}
      this.dialogFormVisible = true;
      this.title = "添加员工"
    },
    // 修改
    handleUpdate(row) {
      this.dialogFormVisible = true;
      this.courseForm = row
      this.title = "编辑员工"
    },
    // 确认
    addCourse(formName) {
      // 员工名称不能为空
      // if (this.courseForm.staffName === '') {
      //   this.$message({
      //     message: '员工名称不能为空!',
      //     type: 'error'
      //   });
      //   return false
      // }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === "编辑员工") {
            edit(this.courseForm).then(res => {
              this.$message({
                message: '成功编辑信息!',
                type: 'success'
              });
              this.dialogFormVisible = false
              this.fetchData()
            })
          } else if (this.title === "添加员工") {
            add(this.courseForm).then(res => {
              this.$message({
                message: '成功添加!',
                type: 'success'
              });
              this.fetchData()
            })
          }
        } else {
          return false
        }
        this.dialogFormVisible = false
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否确认解雇名称为['+row.staffName+']的员工?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id:row.staffId
        }
        del(params).then(res => {
          this.$message({
            type: 'success',
            message: '完成解雇!'
          });
          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消解雇!'
        });
      });
    },
    handleSelectionChange(selectionRowArr) {
      this.ids = [];
      for (let i = 0; i < selectionRowArr.length; i++) {
        this.ids.push(selectionRowArr[i].staffId)
      }
    },
    delBatch() {
      this.$confirm('是否确认删除编号为['+this.ids+']的数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delBatch({ids:this.ids.join(",")}).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    uploadExcelSuccess() {
            this.$message({
                type: "success",
                message: "上传成功"
            })
            this.dialogExcelVisible = false;
            this.fetchData();
        },

    // 上传excel文件失败
    uploadExcelError(err) {
            this.$message({
                type: "error",
                message: err
            })
        },
    // 导出
    downloadExcel() {
      this.$confirm('是否确认导出所有数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        window.location.href = 'http://localhost:8081/psm/staff/download'

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消导出'
        })
      })
    },


  },
  created() {
    this.fetchData()
    this.findKuang()
  }
}
</script>

<style>

</style>
